package com.javafamily.controller;

import com.javafamily.bean.Pets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/pets")
public class PetsController {

    @Autowired
    private RestTemplate restTemplate;

    // 主机 + 端口号
    // private static final String SERVICE_PROVIDER = "http://localhost:8081";
    // 提供者的微服务名称
    private static final String SERVICE_PROVIDER = "http://familycloud-provider-depart";

    @PostMapping("/save")
    public boolean saveHandler(@RequestBody Pets pets) {
        // 消费者连接提供者端口号
        String url = SERVICE_PROVIDER + "/pets/save";
        return restTemplate.postForObject(url, pets, Boolean.class);
    }

    @DeleteMapping("/delete/{id}")
    public void deleteHandler(@PathVariable("id") int id) {
        // 消费者连接提供者端口号
        String url = SERVICE_PROVIDER + "/pets/delete/" + id;
        restTemplate.delete(url);
    }

    @PutMapping("/update")
    public void updateHandler(@RequestBody Pets pets) {
        // 消费者连接提供者端口号
        String url = SERVICE_PROVIDER + "/pets/update";
        restTemplate.put(url, pets);
    }


    @GetMapping("/get/{id}")
    public Pets getByIdHandler(@PathVariable("id") int id) {
        // 消费者连接提供者端口号
        String url = SERVICE_PROVIDER + "/pets/get/" + id;
        return restTemplate.getForObject(url, Pets.class);
    }

    @GetMapping("/list")
    public List<Pets> listHandler() {
        // 消费者连接提供者端口号
        String url = SERVICE_PROVIDER + "/pets/list";
        return restTemplate.getForObject(url, List.class);
    }

}
